package com.ym.controller.management;

import com.github.pagehelper.PageInfo;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;

import com.ym.medical.domain.entity.CouponEntity;
import com.ym.medical.domain.param.CouponEditParam;
import com.ym.medical.domain.param.CouponParam;
import com.ym.medical.domain.param.CouponQryParam;
import com.ym.medical.domain.vo.CouponVo;
import com.ym.medical.service.ICouponSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 张文博
 * @version: 2019年08月30日 10:43
 */
@Api(tags = "优惠券")
@Controller
@RequestMapping("/management/coupon")
public class CouponController extends BaseController {

	@Autowired
	private ICouponSV iCouponSV;


	@ApiOperation("优惠券添加")
	@PostMapping("/add/coupon")
	@ResponseBody
	public APIResponse<CouponEntity> addCoupon(
			@ApiParam(name = "coupon", value = "券对象", required = true)
			@RequestBody CouponQryParam coupon) {
		Long currId = ShiroUtil.getLoginId();
		coupon.setCreatePerson(currId);
		int rows = iCouponSV.addCoupon(coupon);
		if (rows != 1) {
			return successMsg("添加失败");
		} else {
			return APIResponse.success();
		}

	}

	@ApiOperation("优惠券列表")
	@GetMapping("/coupon/list")
	@ResponseBody
	public APIResponse<PageInfo<CouponVo>> couponList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "model", value = "model对象", required = false)
					CouponParam couponModel) {
		PageInfo<CouponVo> couponPage = iCouponSV.couponList(pageNo, limit, couponModel);
		return successData(couponPage);
	}

	@ApiOperation("优惠券更改")
	@PostMapping("/update/coupon")
	@ResponseBody
	public APIResponse<CouponEntity> updateCoupon(@RequestBody CouponEditParam coupon) {
		int rows = iCouponSV.updateCoupon(coupon.covert());
		if (rows == 0) {
			return successMsg("更改失败");
		}
		return successMsg("更改成功");
	}


	@ApiOperation("优惠券删除")
	@PostMapping("/delete/coupon")
	@ResponseBody
	public APIResponse deleteCoupon(@RequestBody Long[] ids) {
		int rows = 0;
		if (ids == null) {
			throw new BusinessException("请选择");
		}
		rows = iCouponSV.deleteByIds(ids);
		if (rows == 0) {
			return successMsg("删除失败");
		} else {
			return successMsg("删除成功");
		}
	}


}
